package com.kili.recommend.data.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kili.recommend.data.config.ProjectEnum;
import com.kili.recommend.data.service.UserService;
import com.kili.recommend.data.entity.User;
import com.kili.recommend.data.mapper.UserMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author fenny
 * @since 2019-08-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    @DS("#projectEnum")
    public int put(ProjectEnum projectEnum, User user) {
        return getInfo(user.getUserId()) > 0 ? 0 : baseMapper.insert(user);
    }

    @Cacheable(value = "targetClass + methodName + #p0", unless = "#result == null")
    public int getInfo(long userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(User.USER_ID, userId);
        return this.baseMapper.selectCount(queryWrapper);
    }

}
